﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
namespace WPFExcelReport
{ 
    public class ITXAPSData : BaseFunction
    {
        public string KSTKRS = "";
        public string KST = "";
        public string AMTYP = "";
        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        public INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        SETINTEXDB INTEXDB = new SETINTEXDB();
        public ITXAPSData()
            : base()
        {
            Helpers.Helper.GetDate(ref date, ref year, ref month, ref day);
        }
        public DataSet LoadEnv()
        {
            DataSet SResult = new DataSet();
            DataSet SReturn = new DataSet();
            DataRow dwData;
          
            dwData = INTEXDB.AMSTAMM.NewRow();
            dwData["AMMANDANT"] = "00";
            dwData["AMKSTKRS"] = KSTKRS;
            dwData["AMKST"] = KST;
            dwData["AMTYP"] = AMTYP;
            SResult = INTEXDBC.QueryAMSTAMM(dwData, 1);
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                row["AMBEZ"] = Helpers.Helper.GetGB2312Value(row["AMBEZ"].ToString().Trim()); 
                row["AMAENDZL"] = 1;//排产最后日期
                row["AMAENDDAT"] = 0;//排产最后日期
                row["AMNEUDAT"] = "0000";//排产最后时间
            }
            SReturn.Merge(SResult);
            INTEXDBC.DeleteICC_STDMACHINE(null, 1);
            INTEXDBC.InsertICC_STDMACHINE(null, 1);
            SResult = INTEXDBC.QueryICC_STDMACHINE(dwData, 1); 
            SReturn.Merge(SResult);
            SResult = INTEXDBC.QueryICC_SRULES(null, 9999);
            SReturn.Merge(SResult);
            SResult = INTEXDBC.QueryICC_PLANSETUP(null, 9999);
            SReturn.Merge(SResult);
            return SReturn;
        }
        public DataSet BuildData(DataRow dwInput)
        {
            DataSet Result = new DataSet();
            DataSet Return = new DataSet();
            DataRow dwABKOPF = null;
            string KRS = "";
            string LFD = "";
            try
            {
                EventDispatcher.Instance.RaiseStatus("正在读取排产数据", StatusEventType.StartProgress);
                DataRow dwData = null;
                #region=======ICC_MKRF=======
                dwData = INTEXDB.ICC_QUERYTB.NewRow();
                dwData["ABFMANDANT"] = dwInput["ABFMANDANT"];
                dwData["ABFKSTKRS"] =KSTKRS;
                dwData["ABFKST"] = KST;
                Result = INTEXDBC.QueryICC_MKRF(dwData, 2);
                foreach (DataRow row in Result.Tables[0].Rows)
                {
                    if (row["VKKLINES"].ToString() == "")
                    {
                        row["VKKLINES"] = "1";
                    }
                    if (row["JWDRATIO"].ToString() == "")
                    {
                        row["JWDRATIO"] = "1";
                    }
                    if (row["JDRATIO"].ToString() == "")
                    {
                        row["JDRATIO"] = "1";
                    }
                    if (row["WDRATIO"].ToString() == "")
                    {
                        row["WDRATIO"] = "1";
                    }
                    if (row["JWDNUM"].ToString() == "")
                    {
                        row["JWDNUM"] = "1";
                    }
                    if (row["WDNUM"].ToString() == "")
                    {
                        row["WDNUM"] = "1";
                    }
                    if (row["JDNUM"].ToString() == "")
                    {
                        row["JDNUM"] = "1";
                    }
                    if (row["JWNUM"].ToString() == "")
                    {
                        row["JWNUM"] = "1";
                    }
                    if (row["WNUM"].ToString() == "")
                    {
                        row["WNUM"] = "1";
                    }
                    if (row["JNUM"].ToString() == "")
                    {
                        row["JNUM"] = "1";
                    }
                    if (row["RVKKLINES"].ToString() == "")
                    {
                        row["RVKKLINES"] = "1";
                    }
                    if (row["SVKKLINES"].ToString() == "")
                    {
                        row["SVKKLINES"] = "1";
                    }

                }
                Return.Merge(Result);
                Result = INTEXDBC.QueryICC_MKRF(dwData, 3);
                Result.Tables[0].TableName = "BMKRF"; 
                Return.Merge(Result);
                #endregion
                #region=======QueryICC_PVKPOS======= 
                dwData["VKPMANDANT"] = dwInput["ABFMANDANT"];
                Result = INTEXDBC.QueryICC_PVKPOS(dwData, 1);
                Return.Merge(Result);
                #endregion
                #region=======QueryICC_JWYARN=======  
                dwData["VKPMANDANT"] = dwInput["ABFMANDANT"];
                Result = INTEXDBC.QueryICC_JWYARN(dwData, 2);
                Return.Merge(Result); 
                #endregion
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                EventDispatcher.Instance.RaiseStatus("完成读取排产数据", StatusEventType.StopProgress);
            }
            return Return;
        } 
        
    }
}